<link href="<?php echo base_url('theme/css/frontend/company-add.css'); ?>" rel="stylesheet">
<link href="<?php echo base_url('theme/css/frontend/account-manager.css'); ?>" rel="stylesheet">
<link href="<?php echo base_url('theme/css/frontend/search.css'); ?>" rel="stylesheet">
<script src="<?php echo base_url('theme/js/frontend/jquery.trialPhoto.js'); ?>" type="text/javascript"></script>
<script src="<?php echo base_url('theme/js/frontend/jquery.ajax.upload.js'); ?>"></script>
<script src="<?php echo base_url('theme/js/frontend/jquery.validate.js'); ?>"></script>
<script type="text/javascript">
    $(document).ready(function() {

    });
    function cancelevent(id) {
        var r = confirm("Bạn có chắc chắn muốn huỷ sự kiện này?");
        if (r == true) {
            window.location = "/event/delete/" + $('input[name=id]').val();
        }
    }

    function join(event_id) {
        $.ajax({
            url: '/event/join/' + event_id,
            type: "POST",
            dataType: 'json',
            success: function(data) {
                if (typeof(data['error']) != "undefined") {
                    $("button[name=join][tag=" + event_id + "]").hide();
                    $("button[name=notjoin][tag=" + event_id + "]").hide();
                    $(".join-message[tag=" + event_id + "]").text(data['message']);
                } else {
                    $("button[name=join][tag=" + event_id + "]").hide();
                    $("button[name=notjoin][tag=" + event_id + "]").hide();
                    $(".join-message[tag=" + event_id + "]").text("Đăng ký tham gia thành công.");
                }
            }
        });
    }

    function notjoin(event_id) {
        $.ajax({
            url: '/event/notjoin/' + event_id,
            type: "POST",
            dataType: 'json',
            success: function(data) {
                if (typeof(data['error']) != "undefined") {
                    $("button[name=join][tag=" + event_id + "]").hide();
                    $("button[name=notjoin][tag=" + event_id + "]").hide();
                    $(".join-message[tag=" + event_id + "]").text(data['message']);
                } else {
                    $("button[name=join][tag=" + event_id + "]").hide();
                    $("button[name=notjoin][tag=" + event_id + "]").hide();
                    $(".join-message[tag=" + event_id + "]").text("Từ chối tham gia thành công.");
                }
            }
        });
    }

    function change(id, type) {
        if (type == 1) {
            window.location = "/event/company/" + id;
        } else if (type == 2) {
            window.location = "/event/booth/" + id;
        }
    }
</script>
<div class="management-container booth-container search-container">
    <?php echo validation_errors('<div class="alert alert-error">', '</div>'); ?>
    <div id="search-top">
        <div class="search-left">
            <h3 style="color:#F8A324;">SỰ KIỆN DIỄN RA TRONG NĂM <?php echo $year; ?></h3>
        </div>
        <div class="search-right">
            <div class="arrow_left">
                <?php if(isset($cid)) { ?>
                    <a class="link-arrow-left" href="<?php echo base_url('/event/search/' . $prev_year . '/' . $prev_month . '/' . $cid . '/' . $type); ?>"></a>	
                <?php } else { ?>
                    <a class="link-arrow-left" href="<?php echo base_url('/event/search/' . $prev_year . '/' . $prev_month); ?>"></a>	
                <?php } ?>
            </div>
            <div class="search-right-content">
                <h3 style="color:#F8A324;"><?php echo $month . " " . $year; ?></h3>	
            </div>
            <div class="arrow_right">
                <?php if(isset($cid)) { ?>
                    <a  class="link-arrow-right" href="<?php echo base_url('/event/search/' . $next_year . '/' . $next_month . '/' . $cid . '/' . $type); ?>"></a>		
                <?php } else { ?>
                    <a  class="link-arrow-right" href="<?php echo base_url('/event/search/' . $next_year . '/' . $next_month); ?>"></a>		
                <?php } ?>
            </div>
        </div>
    </div>
    <hr class="hr hr-search" />
    <div id="search-content">
        <?php if (!empty($event)) { ?>
            <?php foreach ($event as $ev) { ?>
                <div class="event-info">
                    <div class="event-info-left">
                        <?php if (!empty($ev['image'])) { ?>
                            <img src="<?php echo base_url($ev['image']); ?>" />
                        <?php } else { ?>
                            <img src="<?php echo base_url('theme/images/sample/event/340x184.jpg'); ?>" />	
                        <?php } ?>
                    </div>
                    <div class="event-info-right">
                        <div class="event-info-right-content">
                            <h3><?php echo $ev['name']; ?></h3>
                            <p class="address">
                                <?php echo $ev['address']; ?>
                            </p>
                            <p class="participation">
                                <?php echo $ev['count']; ?> người tham gia sự kiện
                            </p>
                            <p class="date">
                                <?php echo $ev['day'] . ": " . $ev['time_event']; ?>
                            </p>
                        </div>
                        <?php if (isset($ev['current_user']) && $ev['current_user'] == 1) { ?>
                            <div class="button" style="margin-bottom: 10px;margin-left: 250px;">
                                <button class="btn btn-primary btn-event-cancel" type="button" onclick="cancelevent(<?php echo $ev['id']; ?>);">
                                    Huỷ
                                </button>
                                <button class="btn btn-primary btn-event" name="change" onclick="change(<?php echo $ev['id'] . "," . $ev['type']; ?>);" value="Sửa đổi">
                                    Sửa đổi
                                </button>
                            </div>
                        <?php } else { ?>
                            <?php if (isset($ev['current_participation']) && $ev['current_participation'] == 1) { ?>
                                <div class="join-message" style="margin-bottom: 10px;margin-left: 250px;">Bạn đã đăng ký tham gia sự kiện này.</div>
                            <?php } else if (isset($ev['current_not_participation']) && $ev['current_not_participation'] == 1) { ?>
                                <div class="join-message" style="margin-bottom: 10px;margin-left: 250px;">Bạn đã từ chối tham gia sự kiện này.</div>
                            <?php } else if(isset($_SESSION['userLoggedIn'])) { ?>
                                <div class="button" style="margin-bottom: 10px;margin-left: 250px;">
                                    <button class="btn btn-primary btn-event-cancel" name="join" tag="<?php echo $ev['id']; ?>" type="button" onclick="join(<?php echo $ev['id']; ?>);">
                                        Tham gia
                                    </button>
                                    <button class="btn btn-primary btn-event" name="notjoin" tag="<?php echo $ev['id']; ?>" onclick="notjoin(<?php echo $ev['id']; ?>);" value="Sửa đổi">
                                        Không tham gia
                                    </button>
                                    <div class="join-message" tag="<?php echo $ev['id']; ?>"></div>
                                </div>
                            <?php } else if(!isset($_SESSION['userLoggedIn'])) { ?>
                                <div class="join-message" style="margin-bottom: 10px;margin-left: 250px;">Bạn cần đăng nhập để tham gia sự kiện này.</div>
                            <?php } ?>
                        <?php } ?>
                    </div> 
                </div>
                <?php
            }
        } else {
            ?>
            <p style="margin-left:20px;">Không tìm thấy bản ghi phù hợp yêu cầu.</p>
        <?php } ?>
    </div>

    <!-- /management-wrapper -->
</div>